// pages/mch/conf/index.js
const App = getApp()
import Util from '../../../utils/util'
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'

Page({
    data: {
        init: false,

        mer: {},
        conf: {},

        unkeepOpt: ['就近4S店', '保险公司'],
        unkeepIdx: 0,

        ope_opb: '',
        ope_opc: '',
        ope_address: '',
        ope_phone: '',
        ope_name: '',
        ope_short_name: '',
        ope_pri_account_num: '',

        svc_onsite_flag: false,
        svc_onsite_meter: 0,
        svc_onsite_cost: 0,
        svc_onsite_mile: 0,

        svc_remote_flag: false,
        svc_remote_meter: 0,
        svc_remote_cost: 0,
        svc_remote_mile: 0,

        svc_bins_flag: false,
        svc_bins_cost: 0,
        svc_bins_max: 0,

        svc_dins_flag: false,
        svc_dins_cost: 0,
        svc_dins_max: 0,

        svc_tins_flag: false,
        svc_tins_quota: 0,
        svc_tins_cost: 0,

        svc_free_flag: false,
        svc_free_quota: 0,
        svc_free_cost: 0,

        svc_night_flag: false,
        svc_night_cost: 0,

        svc_waive_flag: false,

        con_point_cost: 0,
        con_fine_cost: 0,
        con_invoice_per: 0,
        con_unkeep_info: '',
        con_down_per: 0,
        con_wreck_per: 0,
        con_timeout_half: 0,
        con_timeout_day: 0,
        con_late_per: 0,
        con_force_day: 0,
        con_fuel_cost: 0,
        con_unfreeze_day: 0,
        con_violation_day: 0,
        con_lawsuit_day: 0,
        con_court_info: '',
        other_items: [],

        fake: false,
        createWxAppDialog: false,

        showA: false,
        showB: false,
        showTable: [],
        valueT: '',
        contractstate: false
    },
    // 开启自动签订
    // goooo(){

    // },

    showPaper() {

    },
    onLoad() {
        this.execLoad()
    },
    doSwitchChangeFake(e) {
        const tips = e.currentTarget.dataset.tips
        wx.showToast({
            title: tips,
            icon: 'none'
        })
        this.setData({
            fake: false
        })
    },
    execLoad() {
        App.doPost({
            uri: '/app/merchant/conf/detail',
            body: {},
            fnSucc: resp => {
                this.setData({
                    init: true,
                    mer: resp.data,
                    conf: resp.data.city,

                    ope_opb: resp.data.operate.opb,
                    ope_opc: resp.data.operate.opc,
                    ope_address: resp.data.operate.address,
                    ope_phone: resp.data.operate.bus_phone_num,
                    ope_name: resp.data.operate.full_name,
                    ope_short_name: resp.data.operate.short_name,
                    ope_pri_account_num: resp.data.operate.pri_account_num,

                    svc_onsite_flag: resp.data.service.onsite.flag,
                    svc_onsite_meter: resp.data.service.onsite.meter / 1000,
                    svc_onsite_cost: resp.data.service.onsite.cost / 100,
                    svc_onsite_mile: resp.data.service.onsite.mile / 100,

                    svc_remote_flag: resp.data.service.remote.flag,
                    svc_remote_meter: resp.data.service.remote.meter / 1000,
                    svc_remote_cost: resp.data.service.remote.cost / 100,
                    svc_remote_mile: resp.data.service.remote.mile / 100,

                    svc_bins_flag: resp.data.service.bins.flag,
                    svc_bins_cost: resp.data.service.bins.cost / 100,
                    svc_bins_max: resp.data.service.bins.max / 100,

                    svc_dins_flag: resp.data.service.dins.flag,
                    svc_dins_cost: resp.data.service.dins.cost / 100,
                    svc_dins_max: resp.data.service.dins.max / 100,

                    svc_tins_flag: resp.data.service.tins.flag,
                    svc_tins_quota: resp.data.service.tins.quota,
                    svc_tins_cost: resp.data.service.tins.cost / 100,

                    svc_free_flag: resp.data.service.free.flag,
                    svc_free_quota: resp.data.service.free.quota / 100,
                    svc_free_cost: resp.data.service.free.cost / 100,

                    svc_night_flag: resp.data.service.night.flag,
                    svc_night_cost: resp.data.service.night.cost / 100,

                    svc_waive_flag: resp.data.service.waive.flag,

                    con_point_cost: resp.data.contract.point.cost / 100,
                    con_fine_cost: resp.data.contract.fine.cost / 100,
                    con_invoice_per: resp.data.contract.invoice.per / 10,
                    unkeepIdx: this.data.unkeepOpt.findIndex(s => s == resp.data.contract.unkeep.info),
                    con_down_per: resp.data.contract.down.per / 10,
                    con_wreck_per: resp.data.contract.wreck.per / 10,
                    con_timeout_half: resp.data.contract.timeout.half,
                    con_timeout_day: resp.data.contract.timeout.day,
                    con_late_per: resp.data.contract.late.per / 10,
                    con_force_day: resp.data.contract.force.day,
                    con_fuel_cost: resp.data.contract.fuel.cost / 100,
                    con_unfreeze_day: resp.data.contract.unfreeze.day,
                    con_violation_day: resp.data.contract.violation.day,
                    con_lawsuit_day: resp.data.contract.lawsuit.day,
                    con_court_info: resp.data.contract.court.info,
                    // other_items: resp.data.contract.other_items ? resp.data.contract.other_items : [],
                    showTable: resp.data.contract.other_items ? resp.data.contract.other_items : []
                })
            },

        })


        // 默认调用查询自动签开启状态
    },
    doUnkeepChange(e) {
        this.setData({
            unkeepIdx: parseInt(e.detail.value)
        })
    },
    showTip() {
        wx.showToast({
            title: '此手机号可与登录账号不一致，用于接收订单信息短信及电话',
            icon: 'none'
        })
    },
    doSwitchChange(e) {
        let odata = {}
        odata[e.currentTarget.dataset.field] = !this.data[e.currentTarget.dataset.field]
        this.setData({
            ...odata
        })
    },
    doCheckInput(e) {
        let value = parseFloat(e.detail.value || '0')
        let min = parseFloat(e.target.dataset.min || '0')
        let max = parseFloat(e.target.dataset.max || '99999')
        let input = undefined
        if (value < min) {
            input = min + ''
            Util.Toast.fail(`最小值 ${min}`)
        } else if (value > max) {
            input = max + ''
            Util.Toast.fail(`最大值 ${max}`)
        } else {
            input = parseFloat(value.toFixed(2)) + ''
        }
        let odata = {}
        odata[e.currentTarget.dataset.field] = input
        this.setData({
            ...odata
        })
    },
    setRules() {
        if (this.data.showB == false) {
            this.setData({
                showB: true
            })
        } else {
            this.setData({
                showB: false
            })
        }
    },
    addTrue() {
        if (this.data.valueT !== '') {
            this.data.showTable.push(this.data.valueT)
            this.setData({
                showTable: this.data.showTable,
                valueT: ''
            })
            this.onClose()
        } else {
            this.onClose()
        }

    },
    showPaper(e) {
        this.setData({
            contractstate: e.detail.value
        })
        console.log(e.detail.value, "vuuuuu")
        if (this.data.contractstate == true) {
            Dialog.confirm({
                title: "当前将前往开启自动签订页面",
                context: ''
            }).then(() => {
                this.setData({
                    contractstate: true
                })
                // wx.redirectTo({
                // 	url: `/pages/funcs/fdd?src=apply&url=${encodeURIComponent(resp.data.cert)}`,
                // })
            }).catch(() => {
                this.setData({
                    contractstate: false
                })
            })
        }
    },
    deletD(e) {
        let num = e.currentTarget.dataset.arrnum
        Dialog.confirm({
            title: "是否确认删除？",
            context: this
        }).then(() => {
            this.data.showTable.splice(num, 1)
            this.setData({
                showTable: this.data.showTable
            })
        })


    },
    deleteRules(e) {
        const {
            index
        } = e.currentTarget.dataset
        const data = this.data.other_items
        data.splice(index, 1)
        this.setData({
            other_items: data
        })
    },
    bindinput(e) {
        const value = e.detail.value;
        const {
            index
        } = e.currentTarget.dataset
        const other_items = this.data.other_items;
        other_items[index] = value;
        this.setData({
            other_items
        });
        return value

    },
    execSave() {
        if (this.data.loading) {
            return
        }
        this.setData({
            loading: true
        })
        App.doPost({
            uri: '/app/merchant/conf/save',
            body: {
                service: {
                    onsite: {
                        flag: this.data.svc_onsite_flag && this.data.conf.service.onsite.flag,
                        meter: parseInt(parseFloat(this.data.svc_onsite_meter) * 1000),
                        cost: parseInt(parseFloat(this.data.svc_onsite_cost) * 100),
                        mile: parseInt(parseFloat(this.data.svc_onsite_mile) * 100),
                        rate: this.data.mer.service.onsite.rate,
                        must: this.data.mer.service.onsite.must
                    },
                    remote: {
                        flag: this.data.svc_remote_flag && this.data.conf.service.remote.flag,
                        meter: parseInt(parseFloat(this.data.svc_remote_meter) * 1000),
                        cost: parseInt(parseFloat(this.data.svc_remote_cost) * 100),
                        mile: parseInt(parseFloat(this.data.svc_remote_mile) * 100),
                        rate: this.data.mer.service.remote.rate,
                        must: this.data.mer.service.remote.must
                    },
                    bins: {
                        flag: this.data.svc_bins_flag && this.data.conf.service.bins.flag,
                        cost: parseInt(parseFloat(this.data.svc_bins_cost) * 100),
                        max: parseInt(parseFloat(this.data.svc_bins_max) * 100),
                        rate: this.data.mer.service.bins.rate,
                        must: this.data.mer.service.bins.must
                    },
                    dins: {
                        flag: this.data.svc_dins_flag && this.data.conf.service.dins.flag,
                        cost: parseInt(parseFloat(this.data.svc_dins_cost) * 100),
                        max: parseInt(parseFloat(this.data.svc_dins_max) * 100),
                        rate: this.data.mer.service.dins.rate,
                        must: this.data.mer.service.dins.must
                    },
                    tins: {
                        flag: this.data.svc_tins_flag && this.data.conf.service.tins.flag,
                        quota: parseInt(parseFloat(this.data.svc_tins_quota)),
                        cost: parseInt(parseFloat(this.data.svc_tins_cost) * 100),
                        rate: this.data.mer.service.tins.rate,
                        must: this.data.mer.service.tins.must
                    },
                    free: {
                        flag: this.data.svc_free_flag && this.data.conf.service.free.flag,
                        quota: parseInt(parseFloat(this.data.svc_free_quota) * 100),
                        cost: parseInt(parseFloat(this.data.svc_free_cost) * 100),
                        rate: this.data.mer.service.free.rate,
                        must: this.data.mer.service.free.must
                    },
                    night: {
                        flag: this.data.svc_night_flag && this.data.conf.service.night.flag,
                        begin: 22,
                        close: 7,
                        cost: parseInt(parseFloat(this.data.svc_night_cost) * 100),
                        rate: this.data.mer.service.night.rate,
                        must: this.data.mer.service.night.must
                    },
                    waive: {
                        flag: this.data.svc_waive_flag && this.data.conf.service.waive.flag
                    }
                },
                contract: {
                    point: {
                        cost: parseInt(parseFloat(this.data.con_point_cost) * 100),
                    },
                    fine: {
                        cost: parseInt(parseFloat(this.data.con_fine_cost) * 100),
                    },
                    invoice: {
                        per: parseInt(parseFloat(this.data.con_invoice_per) * 10),
                    },
                    unkeep: {
                        info: this.data.unkeepOpt[this.data.unkeepIdx]
                    },
                    down: {
                        per: parseInt(parseFloat(this.data.con_down_per) * 10),
                    },
                    wreck: {
                        per: parseInt(parseFloat(this.data.con_wreck_per) * 10),
                    },
                    timeout: {
                        half: parseInt(parseFloat(this.data.con_timeout_half)),
                        day: parseInt(parseFloat(this.data.con_timeout_day))
                    },
                    late: {
                        per: parseInt(parseFloat(this.data.con_late_per) * 10),
                    },
                    force: {
                        day: parseInt(parseFloat(this.data.con_force_day))
                    },
                    fuel: {
                        cost: parseInt(parseFloat(this.data.con_fuel_cost) * 100),
                    },
                    unfreeze: {
                        day: parseInt(parseFloat(this.data.con_unfreeze_day))
                    },
                    violation: {
                        day: parseInt(parseFloat(this.data.con_violation_day))
                    },
                    lawsuit: {
                        day: parseInt(parseFloat(this.data.con_lawsuit_day))
                    },
                    court: {
                        info: this.data.con_court_info
                    },
                    other_items: this.data.showTable
                },
                operate: {
                    opb: this.data.ope_opb,
                    opc: this.data.ope_opc,
                    // address: this.data.ope_address,
                    // bus_phone_num: this.data.ope_phone,
                    // full_name: this.data.ope_name,
                    // short_name: this.data.ope_short_name,
                    // pri_account_num: this.data.ope_pri_account_num
                }

            },
            fnSucc: () => {
                Util.Toast.succ(`提交成功`)
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1000);
            },
            fnFail: resp => {
                Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
            },
            fnComp: () => {
                this.setData({
                    loading: false,
                })
            }
        })
    },
    openCreateWxApp() {
        this.setData({
            createWxAppDialog: true
        })
    },
    closeCreateWxApp() {
        this.setData({
            createWxAppDialog: false
        })
    },
    submitCreateWxApp() {
        const userStr = wx.getStorageSync('App.User')
        const user = JSON.parse(userStr)
        console.log('user', user)
        Util.Dialog.confirm(`提交申请后，我们将尽快联系您，请确保手机畅通`, () => {
            // wx.request({
            //   url: App.Global.API + '/app/phone_num/save',
            //   method: 'POST',
            //   data: {
            //     phone_num: user.merchant.mob,
            //   },
            //   success: (res) => {
            //     console.log('res', res)
            //   },
            //   fail: (err) => {
            //     console.log('err', err)
            //   }
            // })
            const params = {
                mobile: user.merchant.mob,
            }
            console.log('params', params)
            App.doPost({
                uri: '/app/phone_num/save',
                body: params,
                fnSucc: () => {
                    Util.Toast.succ('提交成功')
                    // 已提交过申请
                    wx.setStorageSync('submitCreateWxApp', "1")
                    this.setData({
                        createWxAppDialog: false,
                    })
                },
                fnFail: resp => {
                    Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
                },
            })

        })
    },

    showPopup() {
        console.log("9999999")
        this.setData({
            showA: true
        });
    },

    onClose() {
        this.setData({
            showA: false,
            showB: false
        });
    }
})